//
//  PerseusData.m
//  BrowsePerseus
//
//  Created by Junghoon Jee on 11. 3. 10..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PerseusData.h"

#define URL_FORMAT_WITH_BOOK "http://www.perseus.tufts.edu/hopper/text?doc=%@+%@+%d.%d&fromdoc=Perseus:text:%@"
#define URL_FORMAT			 "http://www.perseus.tufts.edu/hopper/text?doc=%@+%@+%d&fromdoc=Perseus:text:%@"

@implementation PerseusData

-(void)initData {
	if (worksOfWriters == nil) {
		worksOfWriters = [[NSMutableDictionary dictionaryWithCapacity:1] retain];
		[worksOfWriters setObject:[NSArray arrayWithObjects:@"개구리", @"구름", @"기사", @"새", 
															@"뤼시스트라테", @"벌", @"부의 신", @"아카르나이 구역민들",
															@"여인들의 민회", @"테스모포리아 축제의 여인들", @"평화", nil] 
						   forKey:@"아리스토파네스"];
	}
	if (booksOfWorks == nil) {
		booksOfWorks = [[NSMutableDictionary dictionaryWithCapacity:0] retain];
	}
	if (urlOfWriter == nil) {
		urlOfWriter = [[NSMutableDictionary dictionaryWithCapacity:[worksOfWriters count]] retain];
		[urlOfWriter setObject:@"Aristoph." forKey:@"아리스토파네스"];
	}
	if (urlOfWork == nil) {
		urlOfWork = [[NSMutableDictionary dictionaryWithCapacity:2] retain];
		[urlOfWork setObject:@"Frogs" forKey:@"개구리"];
		[urlOfWork setObject:@"Cl." forKey:@"구름"];
		[urlOfWork setObject:@"Kn." forKey:@"기사"];
		[urlOfWork setObject:@"Birds" forKey:@"새"];
		[urlOfWork setObject:@"Lys." forKey:@"뤼시스트라테"];
		[urlOfWork setObject:@"Wasps" forKey:@"벌"];
		[urlOfWork setObject:@"Pl." forKey:@"부의 신"];
		[urlOfWork setObject:@"Ach." forKey:@"아카르나이 구역민들"];
		[urlOfWork setObject:@"Eccl." forKey:@"여인들의 민회"];
		[urlOfWork setObject:@"Thes." forKey:@"테스모포리아 축제의 여인들"];
		[urlOfWork setObject:@"Peace" forKey:@"평화"];
	}
	if (textOfWork == nil) {
		textOfWork = [[NSMutableDictionary dictionaryWithCapacity:2] retain];
		[textOfWork setObject:@"1999.01.0031" forKey:@"개구리"];
		[textOfWork setObject:@"1999.01.0027" forKey:@"구름"];
		[textOfWork setObject:@"1999.01.0033" forKey:@"기사"];
		[textOfWork setObject:@"1999.01.0025" forKey:@"새"];
		[textOfWork setObject:@"1999.01.0035" forKey:@"뤼시스트라테"];
		[textOfWork setObject:@"1999.01.0043" forKey:@"벌"];
		[textOfWork setObject:@"1999.01.0039" forKey:@"부의 신"];
		[textOfWork setObject:@"1999.01.0023" forKey:@"아카르나이 구역민들"];
		[textOfWork setObject:@"1999.01.0029" forKey:@"여인들의 민회"];
		[textOfWork setObject:@"1999.01.0041" forKey:@"테스모포리아 축제의 여인들"];
		[textOfWork setObject:@"1999.01.0037" forKey:@"평화"];
	}
}

-(NSInteger) getWriterCount {
	return [worksOfWriters count];
}

-(NSString*) getWriterForIndex: (NSInteger)row {
	NSArray* keys = [worksOfWriters allKeys];
	return [keys objectAtIndex:row];
}

-(NSInteger) getWorkCountForWriter:(NSString *)name {
	if (worksOfWriters == nil)
		return 0;
	return [[worksOfWriters objectForKey:name] count];
}

-(NSString*) getWorkOfWriter:(NSString *)name forRow:(NSInteger)row {
	return [[worksOfWriters objectForKey:name] objectAtIndex: row];
}

-(NSInteger) getIndexOfWork:(NSString *)work ofWriter:(NSString*)writer {
	if (worksOfWriters == nil)
		return 0;
	NSArray* workArray = [worksOfWriters objectForKey:writer];
	return [workArray indexOfObject:work];
}

-(NSString*) getURL:(NSString *)writer forWork:(NSString *)work inBook:(NSInteger)book onLine:(NSInteger)line {
	if (book) {
		return [NSString stringWithFormat:@URL_FORMAT_WITH_BOOK, [urlOfWriter objectForKey:writer], [urlOfWork objectForKey:work], book, line, [textOfWork objectForKey:work]];
	} else {
		return [NSString stringWithFormat:@URL_FORMAT, [urlOfWriter objectForKey:writer], [urlOfWork objectForKey:work], line, [textOfWork objectForKey:work]];
	}
	return @"BAD URL";
}

-(void) releaseData {
	[worksOfWriters release];
	[booksOfWorks release];
	[urlOfWriter release];
	[urlOfWork release];
}

-(void) dealloc {
	[self releaseData];
	[super dealloc];
}
@end
